package com.tally.${module}.controller;

import com.tally.server.dto.${Domain}DTO;
import com.tally.server.dto.PageDTO;
import com.tally.server.dto.ResponseDTO;
import com.tally.server.service.${Domain}Service;
import com.tally.server.utils.ValidatorUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: sy
 * @Date: 2020/11/13 20:13
 * @Description:
 */
@RestController
@RequestMapping("/${domain}")
public class ${Domain}Controller {

    private static final Logger logger = LoggerFactory.getLogger(${Domain}Controller.class);
    public static final String BUSINESS_NAME = "${tableNameCn}";

    @Autowired
    private ${Domain}Service ${domain}Service;

    /**
     * 列表查询
     * @param pageDTO
     * @return
     */
    @PostMapping("/list")
    public ResponseDTO list(@RequestBody PageDTO pageDTO){
        ${domain}Service.list(pageDTO);
        return ResponseDTO.success(pageDTO);
    }

    /**
     * 保存，id有值时更新，无值时新增
     * @param ${domain}DTO
     * @return
     */
    @PostMapping("/save")
    public ResponseDTO save(@RequestBody ${Domain}DTO ${domain}DTO){
        // 保存校验
    <#list fieldList as field>
    <#if field.name!="id" && field.nameHump!="createTime" && field.nameHump!="updateTime" && field.nameHump!="sort">
        <#if !field.nullAble>
        ValidatorUtil.require(${domain}DTO.get${field.nameBigHump}(),"${field.nameCn}");
        </#if>
        <#if (field.length > 0)>
        ValidatorUtil.length(${domain}DTO.get${field.nameBigHump}(),"${field.nameCn}",1,${field.length?c});
        </#if>
    </#if>
    </#list>

        ${domain}Service.save(${domain}DTO);
        return ResponseDTO.success(${domain}DTO);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public ResponseDTO delete(@PathVariable String id){
        ${domain}Service.delete(id);
        return ResponseDTO.success(null);
    }
}
